////////////////////////////////////////////////////////////
//
// This class is hugely based on the sf::Sprite class
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2013 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//    you must not claim that you wrote the original software.
//    If you use this software in a product, an acknowledgment
//    in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//    and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////

#ifndef BASE_ENTITY_HPP
#define BASE_ENTITY_HPP

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <string>

#include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/Transformable.hpp>
#include <SFML/Graphics/Vertex.hpp>
#include <SFML/Graphics/Rect.hpp>

#include <SmegTools/Config.h>

#include "BaseObject.hpp"

#include <SmegTools/Debug/DebugNew.hpp>

////////////////////////////////////////////////////////////
/// Defines
////////////////////////////////////////////////////////////
#define DEFAULT_MAX_SPEED 100.0f

namespace smeg {
    namespace core {
        class EntityManager;
        
////////////////////////////////////////////////////////////
/// Represents a placeable entity with an associated sprite
////////////////////////////////////////////////////////////
class BaseEntity : public BaseObject, public sf::Drawable {
public:
    ////////////////////////////////////////////////////////////
    /// Default Constructor
    ///
    ////////////////////////////////////////////////////////////
                                BaseEntity();
    ////////////////////////////////////////////////////////////
    /// Constructor with a texture
    ///
    /// \param _pTexture :      New texture
    ///
    ////////////////////////////////////////////////////////////
    explicit                    BaseEntity( const sf::Texture* _pTexture );
    ////////////////////////////////////////////////////////////
    /// Constructor with a texture and a rectangle
    ///
    /// \param _pTexture :      New texture
    /// \param _TextRectangle :     Rectangle defining the region of the texture to display
    ///
    ////////////////////////////////////////////////////////////
                                BaseEntity(   const sf::Texture* _pTexture, 
                                                const sf::IntRect& _TextRectangle );
    ////////////////////////////////////////////////////////////
    /// Copy Constructor
    ///
    /// \param _Copy :          Entity to copy from
    ///
    ////////////////////////////////////////////////////////////
                                BaseEntity( const BaseEntity& _Copy );
    ////////////////////////////////////////////////////////////
    /// Destructor
    ///
    ////////////////////////////////////////////////////////////
    virtual                     ~BaseEntity();

    DECLARE_CLONE_FUNC( BaseEntity, BaseObject )
        
    ////////////////////////////////////////////////////////////
    /// Update the entity
    ///
    /// \param _fDeltaTime :    Time since the last frame
    ///
    ////////////////////////////////////////////////////////////
    virtual void                Update( float _fDeltaTime ) {}
    
    ////////////////////////////////////////////////////////////
    /// \brief Change the source texture of the sprite
    ///
    /// The \a texture argument refers to a texture that must
    /// exist as long as the sprite uses it. Indeed, the sprite
    /// doesn't store its own copy of the texture, but rather keeps
    /// a pointer to the one that you passed to this function.
    /// If the source texture is destroyed and the sprite tries to
    /// use it, the behaviour is undefined.
    /// If \a resetRect is true, the TextureRect property of
    /// the sprite is automatically adjusted to the size of the new
    /// texture. If it is false, the texture rect is left unchanged.
    ///
    /// \param _pTexture    New texture
    /// \param _bResetRect  Should the texture rect be reset to the size of the new texture?
    ///
    /// \see GetTexture, SetTextureRect
    ///
    ////////////////////////////////////////////////////////////
    void                        SetTexture( const sf::Texture* _pTexture, 
                                            bool _bResetRect = false );

    ////////////////////////////////////////////////////////////
    /// \brief Set the sub-rectangle of the texture that the sprite will display
    ///
    /// The texture rect is useful when you don't want to display
    /// the whole texture, but rather a part of it.
    /// By default, the texture rect covers the entire texture.
    ///
    /// \param _TextRectangle Rectangle defining the region of the texture to display
    ///
    /// \see GetTextureRect, SetTexture
    ///
    ////////////////////////////////////////////////////////////
    void                        SetTextureRect(const sf::IntRect& _TextRectangle);

    ////////////////////////////////////////////////////////////
    /// \brief Set the global color of the sprite
    ///
    /// This color is modulated (multiplied) with the sprite's
    /// texture. It can be used to colorize the sprite, or change
    /// its global opacity.
    /// By default, the sprite's color is opaque white.
    ///
    /// \param _Color New color of the sprite
    ///
    /// \see GetColor
    ///
    ////////////////////////////////////////////////////////////
    void                        SetColor(const sf::Color& _Color);

    ////////////////////////////////////////////////////////////
    /// \brief Get the source texture of the sprite
    ///
    /// If the sprite has no source texture, a NULL pointer is returned.
    /// The returned pointer is const, which means that you can't
    /// modify the texture when you retrieve it with this function.
    ///
    /// \return Pointer to the sprite's texture
    ///
    /// \see SetTexture
    ///
    ////////////////////////////////////////////////////////////
    const sf::Texture*          GetTexture() const;

    ////////////////////////////////////////////////////////////
    /// \brief Get the sub-rectangle of the texture displayed by the sprite
    ///
    /// \return Texture rectangle of the sprite
    ///
    /// \see SetTextureRect
    ///
    ////////////////////////////////////////////////////////////
    const sf::IntRect&          GetTextureRect() const;

    ////////////////////////////////////////////////////////////
    /// \brief Get the global color of the sprite
    ///
    /// \return Global color of the sprite
    ///
    /// \see SetColor
    ///
    ////////////////////////////////////////////////////////////
    const sf::Color&            GetColor() const;

    ////////////////////////////////////////////////////////////
    /// \brief Get the local bounding rectangle of the entity
    ///
    /// The returned rectangle is in local coordinates, which means
    /// that it ignores the transformations (translation, rotation,
    /// scale, ...) that are applied to the entity.
    /// In other words, this function returns the bounds of the
    /// entity in the entity's coordinate system.
    ///
    /// \return Local bounding rectangle of the entity
    ///
    ////////////////////////////////////////////////////////////
    sf::FloatRect               GetLocalBounds() const;

    ////////////////////////////////////////////////////////////
    /// \brief Get the global bounding rectangle of the entity
    ///
    /// The returned rectangle is in global coordinates, which means
    /// that it takes in account the transformations (translation,
    /// rotation, scale, ...) that are applied to the entity.
    /// In other words, this function returns the bounds of the
    /// sprite in the global 2D world's coordinate system.
    ///
    /// \return Global bounding rectangle of the entity
    ///
    ////////////////////////////////////////////////////////////
    sf::FloatRect               GetGlobalBounds() const;

private:
    ////////////////////////////////////////////////////////////
    /// \brief Draw the sprite to a render target
    ///
    /// \param target Render target to draw to
    /// \param states Current render states
    ///
    ////////////////////////////////////////////////////////////
    virtual void                draw(sf::RenderTarget& target, sf::RenderStates states) const;

    ////////////////////////////////////////////////////////////
    /// \brief Update the vertices' positions
    ///
    ////////////////////////////////////////////////////////////
    void                        UpdatePositions();

    ////////////////////////////////////////////////////////////
    /// \brief Update the vertices' texture coordinates
    ///
    ////////////////////////////////////////////////////////////
    void                        UpdateTexCoords();

    ////////////////////////////////////////////////////////////
    // Member data
    ////////////////////////////////////////////////////////////
    sf::Vertex                  m_Vertices[4]; ///< Vertices defining the sprite's geometry
    const sf::Texture*          m_pTexture;    ///< Texture of the sprite
    sf::IntRect                 m_TextureRect; ///< Rectangle defining the area of the source texture to display
};

    } // namespace core
} // namespace smeg

#include <SmegTools/Debug/DebugNewOff.hpp>

#endif // BASE_ENTITY_HPP
